package com.ebb;

import android.app.Activity;
import android.app.AlertDialog.Builder;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.kinvey.KCSClient;
import com.kinvey.KinveyUser;
import com.kinvey.util.KinveyCallback;

public class EBBKinveyActivity extends Activity implements OnClickListener {
	private EditText etId;
	private EditText etPwd;
	private Button btnSignIn;
	private Button btnSignUp;
	private String msgResponse;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		// pingTest();
		
		initLayout();
	}
	
	private void initLayout() {
		this.etId = (EditText) findViewById(R.id.id_value);
		this.etPwd = (EditText) findViewById(R.id.pwd_value);

		this.btnSignIn = (Button) findViewById(R.id.signin_button);
		this.btnSignUp = (Button) findViewById(R.id.signup_button);

		this.btnSignIn.setId(0x1001);
		this.btnSignUp.setId(0x1002);

		this.btnSignIn.setOnClickListener(this);
		this.btnSignUp.setOnClickListener(this);
	}

	private void pingTest() {
		KCSClient service = ((EBBKinveyApplication) getApplication())
				.getKinveyService();
		service.pingService(new KinveyCallback<Boolean>() {
			public void onFailure(Throwable t) {
				showAlert(EBBKinveyActivity.this, "EBB Kinvey Ping", "Aww snap! wrong credentials");
			}

			public void onSuccess(Boolean b) {
				showAlert(EBBKinveyActivity.this, "EBB Kinvey Ping", "Connection Successful!");
			}
		});
	}

	private void login(String username, String password) {
		EBBVariable.setVal(password);
		KCSClient service = ((EBBKinveyApplication) getApplication()).getKinveyService();
		service.loginWithUsername(username, password);
		KinveyUser currentUser = service.getCurrentUser();
		
		if (currentUser != null) {
			if (currentUser.getAttribute("nickname") != null) {
				msgResponse = "Hello! " + currentUser.getAttribute("nickname").toString();
				Toast.makeText(getBaseContext(), msgResponse, Toast.LENGTH_LONG).show();
				Intent i = new Intent("com.ebb.EBBKinveyActivity2");
				startActivity(i);
			} else {
				Intent i = new Intent("com.ebb.EBBMemberActivity");
				startActivity(i);
			}
		} else {
			showAlert(EBBKinveyActivity.this, "EBB Login", "Failure!");
		}
	}

	/**
	 * Display a simple alert dialog with the given text and title.
	 * 
	 * @param context
	 *            Android context in which the dialog should be displayed
	 * @param title
	 *            Alert dialog title
	 * @param text
	 *            Alert dialog message
	 */
	protected static void showAlert(Context context, String title, String text) {
		Builder alertBuilder = new Builder(context);
		alertBuilder.setTitle(title);
		alertBuilder.setMessage(text);
		alertBuilder.create().show();
	}

	@Override
	public void onClick(View v) {
		String idValue = etId.getText().toString();
		String pwdValue = etPwd.getText().toString();
		switch (v.getId()) {
		case 0x1001: // Sign In
			if (idValue.length() > 0 && pwdValue.length() > 0) {
				login(idValue, pwdValue);
			} else {
				showAlert(EBBKinveyActivity.this, "EBB Login", "Failure!");
			}
			break;
		case 0x1002: // Sign Up
			Intent i = new Intent("com.ebb.EBBKinveyActivity");
			startActivity(i);
			break;
		}
	}

	@Override
	protected void onStart() {
		super.onStart();
		System.out.println("onStart");
	}

	@Override
	protected void onStop() {
		super.onStop();
		System.out.println("onStop");
		etId.setText("");
		etPwd.setText("");
	}
}